package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.SysTenant;
import com.bjb.vr.common.mapper.SysTenantMapper;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.SysTenantVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @author: HuJingBo
 * @time: 2022/6/9 8:23
 */
@Slf4j
@Service
public class SysTenantService extends ServiceImpl<SysTenantMapper, SysTenant> {


    @Autowired
    SysTenantMapper sysTenantMapper;

    /**
     * 获取租户树形列表
     *
     * @return
     */
    public List<SysTenantVo> selectSysTenantTreeList() {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        List<SysTenantVo> sysTenantList = sysTenantMapper.selectSysTenantList();
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        List<SysTenantVo> treeTenantList = null;
        if(sysTenantList!=null && sysTenantList.size()>0) {
            treeTenantList = builTree(sysTenantList);
        }
        return treeTenantList;
    }

    /**
     * 获取已设置基本信息租户树形列表
     *
     * @return
     */
    public List<SysTenantVo> selectConfigSysTenantTree() {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        List<SysTenantVo> sysTenantList = sysTenantMapper.selectConfigSysTenantTree();
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        List<SysTenantVo> treeTenantList = null;
        if(sysTenantList!=null && sysTenantList.size()>0) {
            treeTenantList = builTree(sysTenantList);
        }
        return treeTenantList;
    }

    /**
     * 获取未设置基本信息租户树形列表
     *
     * @return
     */
    public List<SysTenantVo> selectUnConfigSysTenantTree() {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        List<SysTenantVo> sysTenantList = sysTenantMapper.selectUnConfigSysTenantTree();
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        List<SysTenantVo> treeTenantList = null;
        if(sysTenantList!=null && sysTenantList.size()>0) {
            treeTenantList = builTree(sysTenantList);
        }
        return treeTenantList;
    }

    /**
     * 通过租户名查询
     *
     * @param tenantName
     * @return
     */
    public SysTenant queryTenantByTenantName(String tenantName) {
        LambdaQueryWrapper<SysTenant> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysTenant::getTenantName, tenantName);
        SysTenant sysTenant = sysTenantMapper.selectOne(lambdaQueryWrapper);
        return sysTenant;
    }

    /**
     * 通过租户id获取租户信息
     *
     * @param tenantId
     * @return
     */
    public SysTenant selectOneSysTenantByTenantId(String tenantId) {
        LambdaQueryWrapper<SysTenant> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysTenant::getTenantId, tenantId);
        SysTenant sysTenant = sysTenantMapper.selectOne(lambdaQueryWrapper);
        return sysTenant;
    }

    /**
     * 根据账号ID查询租户名称
     *
     * @param accountId
     * @return
     */
    public String queryTenantNameByAccountId(Long accountId) {
        return sysTenantMapper.queryTenantNameByAccountId(accountId);
    }



    //获取根节点(获取所有的父节点)
    public List<SysTenantVo> getRootNode(List<SysTenantVo> sysTenantList) {
        List<SysTenantVo> rootTenantLists =new  ArrayList<SysTenantVo>();
        for(SysTenantVo tenantNode : sysTenantList) {
            if(tenantNode.getParentId()==0) {
                rootTenantLists.add(tenantNode);
            }
        }
        return rootTenantLists;
    }


    //递归，建立子树形结构
    public SysTenantVo buildChilTree(SysTenantVo pNode,List<SysTenantVo> sysTenantList){
        List<SysTenantVo> chilTenants =new  ArrayList<SysTenantVo>();
        for(SysTenantVo tenantNode : sysTenantList) {
            if(tenantNode.getParentId().equals(pNode.getId())) {
                chilTenants.add(buildChilTree(tenantNode,sysTenantList));
            }
        }
        pNode.setChildren(chilTenants);
        return pNode;
    }


    //建立树形结构
    public List<SysTenantVo> builTree(List<SysTenantVo> sysTenantList){
        List<SysTenantVo> tenantVoList =new  ArrayList<SysTenantVo>();
        for(SysTenantVo tenantNode : getRootNode(sysTenantList)) {
            tenantNode=buildChilTree(tenantNode,sysTenantList);
            tenantVoList.add(tenantNode);
        }
        return tenantVoList;
    }

}
